package org.andes.kownledge;

import java.util.ArrayList;
import java.util.List;

import lisp.SExp;

/**
 * Created by IntelliJ IDEA.
 * User: wenfeix
 * Date: 2011-2-4
 * Time: 14:04:38
 * To change this template use File | Settings | File Templates.
 */
public class EnqSet {
    //list of all Enodes for solution PSMs
    private List<Enode> Eqns;

    //list of all Enodes and Qnodes in solution
    private List<BubbleNode> Nodes=new ArrayList<BubbleNode>();
    public List<BubbleNode> getNodes(){
        return Nodes;
    }
    private String Assumpts;
    
//    (defun read-mreadable-EqnSets (Stream EqnIndex Graph Version)
//    		  (loop for S in (read Stream "Error: Malformed Equation Sets.")
//    		      collect (make-EqnSet 
//    			       :Eqns (cond ; switch on file version
//    			               ((null Version) ; old format: try to convert
//    			                  (eqns-to-enodes (collect-indicies->eqns (first S) EqnIndex) Graph))
//    			               ((and (numberp Version) (>= Version 2)) ; new format
//    			                 (map-indicies->bgnodes (first S) Graph)))
//    			       :Nodes  (map-indicies->bgnodes (second S) Graph)
//    			       :Assumpts (third S))))
    
    //sexp:((4 3 5) (4 1 3 0 5 2) ((AXES-FOR AIRCRAFT 0)))
    public void readContent(int version,SExp sexp,BubbleGraph graph,List<eqn> eqns) throws Exception{
         List<SExp> list= sexp.toList();
         if(version>=2){
        	 map_indicies_bgnodes(list.get(0).toList(),graph);
         }else{
        	 eqns_to_enodes(collect_indicies_eqns( list.get(0).toList(),eqns),graph);
             map_indicies_bgnodes( list.get(1).toList(),graph);
         }
        
    }
    
//    defun collect-indicies->eqns (Indicies Eqns)
//    "Given a list of indicies and a list of vars collect the specified vars."
//    (loop for I in Indicies
//        collect (collect-index->eqn I Eqns)))

//    (defun collect-index->eqn (I Eqns)
//    		  "Given a list of indicies and a list of vars collect the specified vars."
//    		  (let ((eqn (nth I eqns)))
//    		    (when (null eqn) (error "Index i=~A larger than ~A eqns~%" 
//    					    i (length eqns)))
//    		    (when (not (= I (Eqn-Index eqn)))
//    		      (error "Incompatible variable index ~A ~A" I Eqns))
//    		    eqn))
//  eqn.cl
	private List<eqn> collect_indicies_eqns(List<SExp> indices, List<eqn> eqns)
			throws Exception {
		List<eqn> myeqns = new ArrayList<eqn>();
		for (SExp sexp : indices) {
			int index = Integer.valueOf(sexp.GetValue()).intValue();
			eqn eqn = collect_index_eqn(index, eqns);
			myeqns.add(eqn);
		}
		return myeqns;
	}

    private eqn collect_index_eqn(int index,List<eqn> eqns) throws Exception {
       eqn eqn= eqns.get(index);
        if(eqn==null)
            throw new Exception();
        else if(eqn.Index()!=index)
             throw new Exception();
        else
            return eqn;
    }
    private void eqns_to_enodes(List<eqn> eqns,BubbleGraph graph){
        for(eqn eqn:eqns){
            Enode enode= graph.match_algebra_enode(eqn.getAlgebra());
            if(enode!=null){
                Eqns.add(enode);
            }
        }

    }
     //Map the specified indicies to the supplied graph nodes.
    public void map_indicies_bgnodes(List<SExp> indices,BubbleGraph graph){
          for(SExp sexp:indices){
             int index=Integer.valueOf(sexp.GetValue()).intValue();
              BubbleNode node=graph.get_nth_bgnode(index);
            Nodes.add(node);
        }
    }

    //return list of all eqns within a solution EqnSet
    public List<eqn> EqnSet_AllEqns(){
        return null;
        
    }


}
